import { request } from "../utils"
import { Toast } from "vant"

/*
  图片上传
  !!!数据转换过程中file丢失,暂未处理
*/
const uploadFile = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/Image/UploadFile",
    data: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("上传图片失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  添加Ta生活
*/
const insertLife = (requestData, cb) => {
  request({
    method: 'POST',
    url: "api/TaLive/PostTaLive",
    data: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Msg)
    } else {
      console.log("添加Ta生活失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取Ta生活列表
*/
const getLife = (requestData, cb) => {
  request({
    url: "api/TaLive/GetNewTheme",
    params: requestData,
    // params: {
    //   userkey: localStorage.getItem("userKey"),
    //   pageSize: 10,
    // },
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取Ta生活列表失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取我的Ta生活列表
*/
const getMyLife = (cb) => {
  request({
    url: "api/TaLive/GetMyTheme",
    params: {
      userkey: localStorage.getItem("userKey"),
      pageSize: 1000
    }
  }).then(res => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取我的Ta生活列表失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取我点赞用户的Ta生活列表
*/
const getMyFavoriteLife = (cb) => {
  request({
    url: "api/TaLive/GetMyFavoriteTheme",
    params: {
      userkey: localStorage.getItem("userKey"),
      pageSize: 1000
    }
  }).then(res => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取我点赞用户的Ta生活列表失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  关注用户
*/
const followUser = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/TaLive/PostFollow",
    data: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("关注用户失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取我关注的用户
*/
const getFollowUsers = (cb) => {
  request({
    url: "api/TaLive/GetMyFollowers",
    params: {
      userkey: localStorage.getItem("userKey"),
    },
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取我关注的用户失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取指定用户的主题列表
*/
const getLifeByUser = (requestData, cb) => {
  request({
    url: "api/TaLive/GetUserThemes",
    params: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取指定用户的主题列表失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取是否关注某用户
*/
const getIsFollow = (requestData, cb) => {
  request({
    url: "api/TaLive/GetIsFollow",
    params: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取是否关注某用户失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取我的Ta生活详情
*/
const getMyLifeDetail = (requestData, cb) => {
  request({
    url: "api/TaLive/GetTaLiveByThemeId",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取我的Ta生活详情失败...")
      Toast({
        message: res.data.Msg,
        duration: 1000
      })
    }
  })
}

/*
  获取Ta生活评论
*/
const getLifeComment = (requestData, cb) => {
  request({
    url: "api/TaLive/GetTaLiveCommentByThemeId",
    params: requestData,
  }).then(res => {
    if (res.data.Success) {
      let list = res.data.Data.reverse()
      list.map(item => {
        item.AddDate = item.AddDate.split('T').join(' ')
        return item
      })
      cb && cb(list)
    } else {
      console.log("获取Ta生活评论失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  添加Ta生活评论
*/
const insertLifeComment = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/TaLive/AddTaLiveComment",
    data: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("添加Ta生活评论失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  收藏
*/
const favoriteLife = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/TaLive/PostAddFavoriteTheme",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      let Msg = res.data.Msg
      cb && cb(Msg)
    } else {
      console.log("收藏Ta生活失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  点赞
*/
const likeLife = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/TaLive/PostZanForTheme",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("点赞Ta生活失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  点赞
*/
const deleteLife = (requestData, cb) => {
  request({
    method: "Delete",
    url: "api/TaLive/DelMyTheme",
    params: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("删除Ta生活失败...")
      Toast(res.data.Msg)
    }
  })
}


export {
  uploadFile,
  getLife,
  insertLife,
  getMyLife,
  getMyFavoriteLife,
  getMyLifeDetail,
  getLifeComment,
  insertLifeComment,
  favoriteLife,
  likeLife,
  deleteLife,
  followUser,
  getFollowUsers,
  getLifeByUser,
  getIsFollow,
}
